//The MIT License (MIT)
//
//Copyright (c) 2018 dote
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of
//this software and associated documentation files (the "Software"), to deal in
//the Software without restriction, including without limitation the rights to
//use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
//the Software, and to permit persons to whom the Software is furnished to do so,
//subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
//FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
//COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
//IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
//CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.github.dote.mio.util;

import com.github.dote.mio.client.UdpClient;
import com.github.dote.mio.client.IOClient;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * 查询复合条件的在线用户
 * @see com.github.dote.mio.filter.QueryImpl
 * @see com.github.dote.mio.filter.IQuery
 */
@Deprecated
public class Query {

    private List<String> concatList = new ArrayList<>();
    private List<String> keyList = new ArrayList<>();
    private List<String> operList = new ArrayList<>();
    private List<Object> valueList = new ArrayList<>();

    public Query() {
    }

    public Query and() {
        concatList.add("and");
        return this;
    }


    public Query eq(String key, Object value) {
        keyList.add(key);
        valueList.add(value);
        operList.add("=");
        return this;
    }

    public Query neq(String key, Object value) {
        keyList.add(key);
        valueList.add(value);
        operList.add("!=");
        return this;
    }

    public List<IOClient> doQuery(Collection<IOClient> allClients) {
        List<IOClient> resultList = new ArrayList<>();
        if (concatList.size() == keyList.size() && concatList.size() == valueList.size() && concatList.size() == operList.size()) {
            Iterator<IOClient> iter = allClients.iterator();
            while (iter.hasNext()) {
                IOClient client = iter.next();
                int check = 0;
                int i = 0;
                for (; i < concatList.size(); i++) {
                    String concat = concatList.get(i);
                    String key = keyList.get(i);
                    String oper = operList.get(i);
                    Object value = valueList.get(i);

                    if (concat.equals("and")) {
                        Object attrV = client.getAttr(key);
                        if(attrV==null)
                            continue;
                        if (oper.equals("=")) {
                            if (attrV.equals(value)) {
                                check++;
                            }
                        } else if (oper.equals("!=")) {
                            if (!attrV.equals(value)) {
                                check++;
                            }
                        }
                    }

                }
                if (check == i) {
                    resultList.add(client);
                }

            }

        }
        return resultList;
    }

}
